package com.gcalsync.cal;

import com.gcalsync.store.Storable;

import java.util.Enumeration;
import java.util.Hashtable;

/**
 * @author Thomas Oldervoll, thomas@zenior.no
 * @author $Author$
 * @version $Rev: 9 $
 * @date $Date$
 * TODO:
 * - write test, observed strange behiour with phoneToGcalId.size() = 8 and gcalIdToPhone.size() = 6
 * - delete records expired before sync period start
 */
public class IdCorrelator {

    public Hashtable phoneIdToGcalId;
    public Hashtable gcalIdToPhoneId;
    //private int[] obsoleteRecords;

    public IdCorrelator(Storable[] idCorrelations) {
        phoneIdToGcalId = new Hashtable(idCorrelations.length);
        gcalIdToPhoneId = new Hashtable(idCorrelations.length);
        for (int i = 0; i < idCorrelations.length; i++) {
            addCorrelation((IdCorrelation) idCorrelations[i]);
        }
    }

    public void printContents() {
        // TODO: remove
        System.out.println("p->g: " + phoneIdToGcalId.size() + ", g->p: " + gcalIdToPhoneId.size());
        for (Enumeration i = phoneIdToGcalId.keys(); i.hasMoreElements();) {
            String phoneId = (String) i.nextElement();
            String gCalId = null;
            String reversePimId = null;
            if (phoneId != null) {
                gCalId = (String) phoneIdToGcalId.get(phoneId);
            }
            if (gCalId != null) {
                reversePimId = (String) gcalIdToPhoneId.get(gCalId);
            }
            System.out.println(phoneId + "->" + gCalId + "<-" + reversePimId);
        }
    }

    public void addCorrelation(IdCorrelation idCorrelation) {
        phoneIdToGcalId.put(idCorrelation.phoneCalId, idCorrelation.gCalId);
        gcalIdToPhoneId.put(idCorrelation.gCalId, idCorrelation.phoneCalId);
    }
}
